package com.huaru.formboot.form.service.impl;


import com.huaru.formboot.form.dao.TableDataMapper;
import com.huaru.formboot.form.entity.Dto.TableData;
import com.huaru.formboot.form.service.TableDataService;
import com.huaru.formboot.form.util.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 表单组件关系表 服务实现类
 * </p>
 *
 * @author wz
 * @since 2021-04-15
 */
@Service
public class TableDataServiceImpl implements TableDataService {
   @Autowired
    private TableDataMapper tableDataMapper;
    @Override
    public List<TableData> getAllTableData() {
        List<TableData> tree = new ArrayList<>();
        try {
            List<TableData> allTableData = tableDataMapper.getAllTableData();
            Tree<String> stringTree = new Tree<>();
            String pid = "-1";
            tree = stringTree.createTree(allTableData, pid);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return tree;
    }

    @Override
    public Integer saveTableData(TableData tableData) {
        tableData.setUpdateTime(LocalDateTime.now());
        return tableDataMapper.updateTableData(tableData);
    }

    @Override
    public Integer insertRootNode(TableData tableData) {
        String tableParentId = "-1";
        TableData table = new TableData(
                UUID.randomUUID().toString(),
                tableParentId,
                tableData.getTableType(),
                tableData.getTableNumber(),
                tableData.getTableCollector(),
                tableData.getTableDataJson(),
                LocalDateTime.now(),
                LocalDateTime.now()
        );
        return tableDataMapper.insertNode(table);
    }

    @Override
    public Integer insertLowerNode(TableData tableData) {
        tableData.setTableId(UUID.randomUUID().toString());
        tableData.setCreateTime(LocalDateTime.now());
        tableData.setUpdateTime(LocalDateTime.now());
        return tableDataMapper.insertNode(tableData);
    }


    @Override
    public Integer deleteTableData(String tableDataId) {
        return tableDataMapper.deleteTableData(tableDataId);
    }

    @Override
    public Integer updateTableData(TableData tableData) {
        tableData.setUpdateTime(LocalDateTime.now());
        return tableDataMapper.updateTableData(tableData);
    }
}
